package com.zhihuitong.meeting.meeting.create;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.utils.TimePickerUtils;
import com.zhihuitong.meeting.R;
import com.zhihuitong.meeting.base.BaseMVPActivity;
import com.zhihuitong.meeting.utils.TimeConstants;
import com.zhihuitong.meeting.utils.TimeUtils;
import com.zhihuitong.meeting.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：guoyuan
 * 时间: 2020/2/24.
 * 文件描述：预约创建会议页面
 * 版本号：V 1.0
 **/
public class CreatMeetingActivity extends BaseMVPActivity<CreatMeetingPresenter> implements CreatMeetingConract.ICreatMeetingView {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.et_subject)
    EditText mEtSubject;
    @BindView(R.id.tv_startTime)
    TextView mTvStartTime;
    @BindView(R.id.tv_endTime)
    TextView mTvEndTime;
    @BindView(R.id.rb_Video)
    RadioButton mRbVideo;
    @BindView(R.id.rb_Voice)
    RadioButton mRbVoice;
    @BindView(R.id.rg_mediaTypes)
    RadioGroup mRgMediaTypes;
    @BindView(R.id.btn_createMeeting)
    Button mBtnCreateMeeting;
    private long mStartTime;
    private long mEndTime;

    public static void start(Context context) {
        Intent intent = new Intent(context, CreatMeetingActivity.class);
        context.startActivity(intent);
    }


    /**
     * 返回子类布局id
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_creat_meeting;
    }

    /**
     * 初始化數據
     */
    @Override
    protected void initData() {

    }

    /**
     * 初始化View
     */
    @Override
    protected void initView() {
        mToolbar.setTitle("创建会议");//设置主标题
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    /**
     * MVP模式下創建Presenter 必須實現
     *
     * @return
     */
    @Override
    protected CreatMeetingPresenter createPresenter() {
        return new CreatMeetingPresenter(this);
    }

    @OnClick({R.id.tv_startTime, R.id.tv_endTime, R.id.btn_createMeeting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_startTime:
                getStartAndEndTime(mTvStartTime, true);
                break;
            case R.id.tv_endTime:
                getStartAndEndTime(mTvEndTime, false);
                break;
            case R.id.btn_createMeeting:
                String subject = mEtSubject.getText().toString().trim();
                if (TextUtils.isEmpty(subject)) {
                    ToastUtils.showToast("请输入会议主题!");
                    return;
                }
                String startTime = mTvStartTime.getText().toString().trim();
                if (TextUtils.isEmpty(startTime)) {
                    ToastUtils.showToast("请选择开始时间!");
                    return;
                }

                String endTime = mTvEndTime.getText().toString().trim();
                if (TextUtils.isEmpty(endTime)) {
                    ToastUtils.showToast("请选择结束时间!");
                    return;
                }

                if (mEndTime <= mStartTime) {
                    ToastUtils.showToast("结束时间不能小于开始时间!");
                    return;
                }
                long lengthTime = TimeUtils.getTimeSpan(mStartTime, mEndTime, TimeConstants.MIN);
                if (lengthTime < 15 || lengthTime > 1440) {
                    ToastUtils.showToast("会议持续时长控制15分钟至1440分钟");
                    return;
                }

                //媒体类型
                String mediaTypes = mRbVideo.isChecked() ? "Video" : "Voice";
                  showLoading();
                //创建会议
                   mPresenter.creatMeeting(startTime, (int) lengthTime, subject, mediaTypes, endTime);
                break;
        }
    }

    /**
     * 修改开始时间和结束时间
     *
     * @param view
     */
    private void getStartAndEndTime(final TextView view, final boolean isStart) {
        TimePickerUtils.showTimePicker(this, TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                view.setText(TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd HH:mm")));
                if (isStart) {
                    mStartTime = date.getTime();
                } else {
                    mEndTime = date.getTime();
                }
            }
        });
    }


    @Override
    public void createMeetingFail(String msg) {
        ToastUtils.showToast(msg);
        hidLoading();
    }

    @Override
    public void createMeetingSuc(MeetingInfo meetingInfo) {
        ToastUtils.showToast("创建成功!");
        finish();
        hidLoading();
    }


}
